package com.egao.customer.service.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.core.utils.FileUploadUtil;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
//import com.egao.customer.mapper.PayHistoryMapper;
import com.egao.customer.entity.PayHistory;
import com.egao.customer.service.PayHistoryService;
import org.springframework.stereotype.Service;
import com.egao.customer.biz.PayHistoryBiz;
import net.sf.json.JSONObject;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
//import javax.transaction.Transactional;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 缴费记录服务实现类
 * @author : Doman
 * @date : 2020-06-14 21:03:22
 * @version ：1.0
 */
@Service
public class PayHistoryServiceImpl /*extends ServiceImpl<PayHistoryMapper, PayHistory> */implements PayHistoryService {

	@Autowired
	private PayHistoryBiz payHistoryBiz;

    /**
    @Override
    public PageResult<PayHistory> listPage(PageParam<PayHistory> page) {
        List<PayHistory> records = baseMapper.listPage(page);
        return new PageResult<>(records, page.getTotal());
    }

    @Override
    public List<PayHistory> listAll(Map<String, Object> page) {
        return baseMapper.listAll(page);
    } */

    @Override
    public JSONObject list(PageResult2<T> page, PayHistory payHistory, User loginUser)  throws Exception{
        return payHistoryBiz.list(page,payHistory,loginUser);
    }

    @Override
    public JSONObject listSummaryJf(PageResult2<T> page, PayHistory payHistory)  throws Exception{
        try {
            List<String> types = new ArrayList<>();
            types.add("停报");
            types.add("缴费");
            types.add("开口费");
            types.add("退款");
            payHistory.setTypes(types);
            if (payHistory.getType().equals("year")) {
                return payHistoryBiz.listSummaryYear(page,payHistory);
            } else if (payHistory.getType().equals("season")) {
                return payHistoryBiz.listSummarySeason(page,payHistory);
            } else if (payHistory.getType().equals("month")) {
                return payHistoryBiz.listSummaryMonth(page,payHistory);
            } else if (payHistory.getType().equals("week")) {
                return payHistoryBiz.listSummaryWeek(page,payHistory);
            } else if (payHistory.getType().equals("day")) {
                return payHistoryBiz.listSummaryDay(page,payHistory);
            } else {
                return null;
            }
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public JSONObject listSummaryYfk(PageResult2<T> page, PayHistory payHistory)  throws Exception{
        try {
            List<String> types = new ArrayList<>();
            if (Utils.isNotEmpty(payHistory.getPicRemove())) {
                types.add(payHistory.getPicRemove());
            } else {
                types.add("预付款");
                types.add("预付缴费");
            }
            payHistory.setTypes(types);
            if (payHistory.getType().equals("year")) {
                return payHistoryBiz.listSummaryYear(page,payHistory);
            } else if (payHistory.getType().equals("season")) {
                return payHistoryBiz.listSummarySeason(page,payHistory);
            } else if (payHistory.getType().equals("month")) {
                return payHistoryBiz.listSummaryMonth(page,payHistory);
            } else if (payHistory.getType().equals("week")) {
                return payHistoryBiz.listSummaryWeek(page,payHistory);
            } else if (payHistory.getType().equals("day")) {
                return payHistoryBiz.listSummaryDay(page,payHistory);
            } else {
                return null;
            }
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public List<PayHistory> list(PayHistory payHistory, User loginUser) {
        return payHistoryBiz.list(payHistory,loginUser);
    }

    @Override
    public PayHistory getById(PayHistory payHistory) {
        return payHistoryBiz.getById(payHistory);
    }

    @Transactional
    @Override
    public JsonResult save(PayHistory payHistory, User loginUser) {
        if (payHistoryBiz.save(payHistory, loginUser)) {
            return JsonResult.ok("添加成功");
        }
        return JsonResult.error("添加失败");
    }

    @Transactional
    @Override
    public JsonResult saveJf(PayHistory payHistory, User loginUser) {
        payHistory.setCompanyId(UserConstant.COMPANY_ID+"");
        payHistory.setType("缴费");
        if (payHistoryBiz.saveJf(payHistory, loginUser)) {
            return JsonResult.ok("缴费成功");
        }
        return JsonResult.error("缴费失败");
    }

    @Transactional
    @Override
    public JsonResult saveWg(PayHistory payHistory, User loginUser) {
        payHistory.setCompanyId(UserConstant.COMPANY_ID+"");
        payHistory.setType("违规");
        if (payHistoryBiz.saveWg(payHistory, loginUser)) {
            return JsonResult.ok("违规添加成功");
        }
        return JsonResult.error("违规失败");
    }

    @Transactional
    @Override
    public JsonResult update(PayHistory payHistory, User loginUser) {
        if (payHistoryBiz.update(payHistory, loginUser)) {
            String[] pic = payHistory.getPicRemove().split(",");
            for(String p: pic) {
                File file = new File(FileUploadUtil.UPLOAD_FILE_DIR,  p);
                if (file.delete()) new File(FileUploadUtil.UPLOAD_SM_DIR, p).delete();
            }
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @Transactional
    @Override
    public JsonResult updateWg(PayHistory payHistory, User loginUser) {
        if (payHistoryBiz.updateWg(payHistory, loginUser)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @Transactional
    @Override
    public JsonResult remove(PayHistory payHistory, User loginUser) {
        if (payHistoryBiz.remove(payHistory, loginUser)) {
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }

}
